import {baseUrl} from './config.js'
import {loginResult,topListResult,topListDetailResult} from './interface'

export const login = () =>{
	return new Promise(resolve=>{
		uni.request({
			url:`${baseUrl}/register/anonimous`,
			method:'POST',
			success:(res:loginResult)=> {
				wx.setStorage({
					key:'cookie',
					data:res.data.cookie
				})
				resolve(res)
			}
		})
	})
}

export const getTopList = () =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/toplist/detail?cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:topListResult)=> {
				resolve(res)
			}
		})
	})
}

export const getTopListDetail = (id:number) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/playlist/detail?id=${id}&cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:topListDetailResult)=> {
				resolve(res)
			}
		})
	})
}

export const getSongDetail = (id:number) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/song/detail?ids=${id}&cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

export const getSongSimiDetail = (id:number) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/simi/song?id=${id}&cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

export const getSongCommentDetail = (id:number) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/comment/music?id=${id}&cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

export const getSongLyricDetail = (id:number) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/lyric?id=${id}&cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

export const getSongUrlDetail = (id:number) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/song/url?id=${id}&cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

export const getSearchHotDetail = () =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/search/hot/detail?cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

export const getSearchWordDetail = (word:string) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/search?keywords=${word}&cookie=${wx.getStorage({
				key:'cookie',success:(res)=>{
					return res.data
				}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

export const getSearchSuggestDetail = (word:string) =>{
	return new Promise((resolve)=>{
		uni.request({
			url:`${baseUrl}/search/suggest?keywords=${word}&type=mobile&cookie=${wx.getStorage({key:'cookie',success:(res)=>{return res.data}})}`,
			method:'GET',
			success:(res:any)=> {
				resolve(res)
			}
		})
	})
}

